﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class DrawLine : MonoBehaviour 
{
	public Camera camera;
	public float lineStartWide = 0.2f;
	public float lineEndWide = 0.2f;
	public bool multiLine = false;
	public LineRenderer lineRenderer;
	
	public List<Vector3> myPoints = new List<Vector3>();
	public List<LineRenderer> lineRendererList = new List<LineRenderer>();
	public List<List<Vector3>> lineList = new List<List<Vector3>>();
	
	void Awake()
	{
		gameObject.GetComponent<CarController>().enabled = false;
	}
	
	// Use this for initialization
	void Start () 
	{
		SetSigleLineRenderer();
	}
	
	void SetSigleLineRenderer()
	{
		lineRenderer.SetWidth(lineStartWide,lineEndWide);
		lineRenderer.SetVertexCount(0);
	}
	
	bool CreateALineRenderer()
	{
		if(lineRenderer == null)
		{
			Debug.LogWarning("lineRenderer null");
			return false;
		}
		else
		{
			// create new LineRenderer
			LineRenderer newlineRenderer = (LineRenderer)Instantiate (lineRenderer);
			newlineRenderer.SetWidth(lineStartWide,lineEndWide);
			newlineRenderer.SetVertexCount(0);
			lineRendererList.Add(newlineRenderer);
			newlineRenderer.name = "line" + (lineRendererList.FindIndex(item=> item == newlineRenderer) +1);
			
			// create new line point
			List<Vector3> pointList = new List<Vector3>();
			lineList.Add(pointList);
			return true;
		}
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(multiLine)
		{
			MultiLine();
		}
		else
		{
			SingleLine();
		}
		
		if(Input.GetKey(KeyCode.Space))
		{
			GetComponent<CarController>().enabled = true;
		}
		
	}
	
	public List<Vector3> getListPoints
	{
		get
		{
			return myPoints;	
		}
	}
	
	public List<List<Vector3>> getAllListPoints
	{
		get
		{
			return lineList;	
		}
	}
	
	void MultiLine()
	{
		
		if(Input.GetMouseButtonDown(0))
		{
			CreateALineRenderer();
			enableAdd = true;
		}
		
     	if(Input.GetMouseButtonUp(0))
		{
			enableAdd = false;
		}
		
		if(lineList.Count==0)
		{
			return;
		}
		
		AddPoint(lineList[lineList.Count-1]);
		
		for(int i = 0; i<lineRendererList.Count;i++)
		{
			if(lineList[i].Count > 0)
			{
		        lineRendererList[i].SetVertexCount(lineList[i].Count);
		        for(int j = 0; j < lineList[i].Count; j++)
				{
		            lineRendererList[i].SetPosition(j, lineList[i][j]);  
		        }
	    	}
			else
				lineRendererList[i].SetVertexCount(0);
		}
		
	}
	
	void SingleLine()
	{
		if(myPoints.Count > 0)
		{
	        lineRenderer.SetVertexCount(myPoints.Count);
	        for(int i = 0; i < myPoints.Count; i++)
			{
	            lineRenderer.SetPosition(i, myPoints[i]);  
	        }
    	}
		else
			lineRenderer.SetVertexCount(0);
		
		if(Input.GetMouseButtonDown(0))
        	enableAdd = true;
			
		AddPoint();
		
     	if(Input.GetMouseButtonUp(0))
        	enableAdd = false;
	}
	
	bool enableAdd = false;
	void AddPoint(List<Vector3> myPoints = null)
	{
		if(enableAdd)
		{
			if(myPoints == null)
				myPoints = this.myPoints;
			//Debug.Log("AddPoint x: " + Input.mousePosition.x + "; y: " + Input.mousePosition.y + "; z: " + Input.mousePosition.z);
			Vector3 tempPos = Input.mousePosition;
			tempPos.z = 10;
			Vector3 Addpoint = camera.ScreenToWorldPoint(tempPos);
			if( myPoints.Count == 0 || Addpoint != myPoints[myPoints.Count-1])
			{
				myPoints.Add(Addpoint);
			}
		}
	}
}
